package com.ctshk.rpc.order.tour.mapper;

import java.time.LocalDate;
import java.util.List;

import com.ctshk.rpc.order.tour.dto.*;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectProvider;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ctshk.common.service.mapper.CtsBaseMapper;
import com.ctshk.rpc.order.tour.entity.SeriesOrder;
import com.ctshk.rpc.order.tour.req.SeriesOrderDetailReq;
import com.ctshk.rpc.order.tour.req.SeriesOrderListReq;
import com.ctshk.rpc.order.tour.req.SeriesOrderListSpanReq;
import com.ctshk.rpc.order.tour.req.SeriesOrderMyGroupIdQueryReq;

/**
 * <p>
 * 系列团订单 Mapper 接口
 * </p>
 *
 * @author 谭响
 * @since 2021-01-18
 */
public interface SeriesOrderMapper extends CtsBaseMapper<SeriesOrder> {

    @SelectProvider(type = SeriesOrderProvider.class, method = "selectCountBySelective")
    SeriesOrderListDTO selectCountBySelective(@Param("req") SeriesOrderListReq seriesOrderListReq);

    @SelectProvider(type = SeriesOrderProvider.class, method = "selectListBySelective")
    IPage<SeriesOrderPageDTO> selectListBySelective(IPage<SeriesOrderListReq> page, @Param("req") SeriesOrderListReq req);

    @SelectProvider(type = SeriesOrderProvider.class, method = "selectListSpanBySelective")
    IPage<SeriesOrderListSpanDTO> selectListSpanBySelective(IPage<SeriesOrderListSpanReq> page, @Param("req") SeriesOrderListSpanReq req);

    @SelectProvider(type = SeriesOrderProvider.class, method = "selectDetailBySelective")
    SeriesOrderDetailDTO selectDetailBySelective(SeriesOrderDetailReq req);

    /**
     * 查询所属系列团团期ID列表
     * @param req
     * @return
     */
    @SelectProvider(type = SeriesOrderProvider.class, method = "queryMyGroupId")
    List<Long> queryMyGroupId(SeriesOrderMyGroupIdQueryReq req);

    /**
     * 查询行程团期最近7天销量
     * @param ids
     * @return
     *
     */
    @Select({"<script>",
            "SELECT schedule_id,Count(schedule_id) as saleCount",
            "FROM t_series_order ",
            "WHERE DATEDIFF(#{now},gmt_create) BETWEEN 0 AND 7 ",
            "AND is_pay = 1",
            "AND order_status = 1",
            "AND is_deleted = 0",
            "AND schedule_id IN ",
            "<foreach collection='ids' item='id' open='(' separator=',' close=')'>",
            "#{id}",
            "</foreach>",
            "GROUP BY schedule_id ",
            "ORDER BY COUNT(schedule_id) DESC",
            "</script>"})
    List<SeriesOrderDTO> selectTripScheduleSaleCount(List<Long> ids, LocalDate now);

    /**
     * C端唤起支付获取订单信息
     * @param id
     * @return
     */
    @Select("SELECT id AS order_number,total_price AS total_fee,salesman_id,pay_end_time AS order_valid_time,trip_title AS subject \n" +
            "FROM t_series_order\n" +
            "WHERE is_deleted = 0 AND id = #{id} AND is_pay = 0")
    OrderPayInfoDTO queryOrderPayInfo(@Param("id") Long id);
}
